<?php
/**
 * OpenSocialHelper is intended to ease the sfOpenSocialPlugin use. With this helper you can include the gadget content's, Javascript and CSS. Usually the symfony framework allows to include css and Javascript from the file "view.yml", in the head HTML section. Or application don't use the default html layout, but an gadget layout( look at HowToConfigure for more information) and so we need to include them in the content. This should be fixed in a future version, but for now to include a custom stylesheet or external Javascript file you should use our method's in the OpenSocialHelper.
 * 
 * @package    helper
 * @author     Daniel Botelho <botelho.daniel@gmail.com>
 */

/*
* Esta função incluiu o conteúdo do gadget
*/
/**
 * This method allow to include a Gadget content into the layout
 *
 * @param string $content The content
 * @return string HTML Content code 
 */
function include_content($content,$type=null,$href=null,$cdata=null,$view=null){
  $content_attrib = sfConfig::get('app_opensocial_content');
  $content ='<![CDATA['.$content.']]>';
  $html = '';
  if(!($type || $href || $cdata || $view) && $content_attrib){
    if( $content['type'] == 'url' )
      $html.= tag("Content",$content_attrib)."\n";
    else
      $html.= content_tag("Content",$content,$content_attrib)."\n";
  }else{
    $module_pram = array();
    if($view){
      $module_pram['view'] = $view;
    }
    if($type){
      
      $module_pram['type'] = $type;
      if($href){
        $module_pram['href'] = $href;
      }
      if($cdata){
        $module_pram['cdata'] = $cdata;
      }
    }
    $html.= content_tag("Content",$content,$module_pram)."\n";
  }
  echo $html;
}

function open_content_section($content_section)
{
  return '<Content '._tag_options($content_section).'><![CDATA[';
}
function close_content_section()
{
  return ']]></Content>';
}

/*
* Esta função permite incluir um javascript à aplicação
* exemplo: <?php echo include_js(null,null); ?>
*/
/**
 * This method is used to include Javascript code located at "web/js"
 *
 * @param string $url The name of the Javascript file located at "web/js"
 * @return string HTML code 
 */
function include_js($src)
{
  $base_url = OSConfig::getApplicationBaseUrl();
  if(substr($base_url, -1) == '/')
    $base_url.='js/';
  else
     $base_url.='/js/';
  return include_remote_js($base_url. $src);
}
/**
 * This method is used to include remote Javascript content
 *
 * @param string $url The javascript URL
 * @param boolean $nl If it should append a newline or not
 * @return string HTML code 
 */
function include_remote_js($src,$nl=true)
{
  $module_pram = array();
  //$content = file_get_contents($src);
  $module_pram['src'] = $src;
  $module_pram['type'] = 'text/javascript';
  return content_tag("script",'',$module_pram).($nl?"\n":'');
}

/* PT: Esta função permite incluir um CSS à aplicação exemplo: <?php echo include_css(http://sghdc.no-ip.org/opensocial/css/css.css); ?>*/
/**
 * This method is used to include CSS code located at "web/css"
 *
 * @param string $url The name of the CSS file located at "web/css"
 * @return string HTML code 
 */
function include_css($stylesheet)
{
  $base_url = OSConfig::getApplicationBaseUrl();
  if(substr($base_url, -1) == '/')
    $base_url.='css/';
  else
     $base_url.='/css/';
 
  return include_remote_css($base_url. $stylesheet);
}
/**
 * This method is used to include remote CSS content
 *
 * @param string $url The Content Style Sheet URL
 * @param boolean $nl If it should append a newline or not
 * @return string HTML code 
 */
function include_remote_css($url,$nl=true)
{
  return "<link href=\"$url\" type=\"text/css\" rel=\"stylesheet\">".($nl?"\n":'');
}

/**
 * This method creates a link to an specified JSFunction.
 *
 * @param string $link The description of the link
 * @param JSFuntion $jsf The javascript function
 * @param string $args Arguments represented as a string example: "arg1,agr2"
 * @return string HTML code 
 */
function add_jsfunction_anchor($link,JSFunction $jsf,$args=null)
{
  $functionName = $jsf->getName();
    
  return "<a href=\"javascript: $functionName($args)\">$link</a>\n";
}
